package avicit.bdp.dlm.rest;

import avicit.bdp.dlm.job.DlmJob;
import avicit.bdp.dlm.service.DlmStatService;
import avicit.bdp.dlm.vo.DataSourceViewVo;
import avicit.bdp.dlm.vo.StateLineChartDataVo;
import avicit.bdp.dlm.vo.StateOverviewVo;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据湖统计REST接口
 */
@RestController
@Api(tags = "数据湖统计REST接口")
@RequestMapping("/api/bdp/dlm/stat")
public class DlmStatRest {

    @Autowired
    private DlmStatService dlmStatService;
    @Autowired
    private DlmJob dlmJob;

    /**
     * 概览基本信息
     * 数据查询时间默认为近7天
     */
    @ApiOperation(value = "概览基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类Id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "统计维度（周/月/年）", name = "dimension", dataType = "String"),
            @ApiImplicitParam(value = "统计类型(结构化/非结构化/分类)", name = "type", dataType = "String")
    })
    @GetMapping("/overview")
    public ResponseMsg<List<StateOverviewVo>> overview(@RequestParam(required = false) String typeId,
                                                       @RequestParam(required = false, defaultValue = "week") String dimension,
                                                       @RequestParam Integer type) {

        ResponseMsg<List<StateOverviewVo>> responseMsg = new ResponseMsg<>();
        List<StateOverviewVo> result = dlmStatService.overview(typeId, type, dimension);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    /**
     * 数据湖大屏数据
     * 数据查询时间默认为近30天
     */
    @ApiOperation(value = "数据湖大屏数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "统计维度（周/月/年）", name = "dimension", dataType = "String"),
            @ApiImplicitParam(value = "统计类型(结构化/非结构化/分类)", name = "type", dataType = "String")
    })
    @GetMapping("/bigScreen")
    public ResponseMsg<StateOverviewVo> bigScreen(@RequestParam(required = false, defaultValue = "month") String dimension,
                                                  @RequestParam Integer type) {

        ResponseMsg<StateOverviewVo> responseMsg = new ResponseMsg<>();
        StateOverviewVo result = dlmStatService.bigScreen(dimension, type);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    /**
     * 概览图表数据
     */
    @ApiOperation(value = "概览图表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "类型", name = "type", dataType = "Int"),
            @ApiImplicitParam(value = "分类Id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "统计维度（周/月/年）", name = "dimension", dataType = "String"),
            @ApiImplicitParam(value = "数据源id", name = "dataSourceId", dataType = "String"),
            @ApiImplicitParam(value = "开始时间", name = "startTime", dataType = "String"),
            @ApiImplicitParam(value = "结束时间", name = "endTime", dataType = "String")
    })
    @GetMapping("/lineChart")
    public ResponseMsg<StateLineChartDataVo> lineChart(@RequestParam Integer type,
                                                       @RequestParam(required = false) String typeId,
                                                       @RequestParam(required = false) String dimension,
                                                       @RequestParam(required = false) String dataSourceId,
                                                       @RequestParam(required = false) String startTime,
                                                       @RequestParam(required = false) String endTime) {

        ResponseMsg<StateLineChartDataVo> responseMsg = new ResponseMsg<>();
        List<String> allSubTypeIdList = null;
        if (StringUtils.isNotBlank(typeId)) {
            allSubTypeIdList = dlmStatService.getAllSubTypeIdList(typeId);
        }
        StateLineChartDataVo result = dlmStatService.lineChart(type, allSubTypeIdList, dimension,
                dataSourceId, startTime, endTime);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    /**
     * 存储查看
     */
    @ApiOperation(value = "存储查看")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数据源id", name = "dataSourceId", dataType = "String"),
            @ApiImplicitParam(value = "分类Id", name = "typeId", dataType = "String")
    })
    @GetMapping("/dataSourceView")
    public ResponseMsg<DataSourceViewVo> dataSourceView(@RequestParam String dataSourceId,
                                                        @RequestParam String typeId) {

        ResponseMsg<DataSourceViewVo> responseMsg = new ResponseMsg<>();
        DataSourceViewVo result = dlmStatService.dataSourceView(typeId, dataSourceId);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    @GetMapping("/testJob")
    public ResponseMsg<String> test() {

        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        try {
            dlmJob.statData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseMsg;
    }


}
